﻿/// <reference path="../../Scripts/jquery-1.7.1-vsdoc.js" />
/// <reference path="../Common.js" />
/// <reference path="../poshytip-1.1/src/jquery.poshytip.min.js" />

$(function () {

    ClearMsg("username");
    ClearMsg("pwd");

    $("#Login").live("click", function () {
        UserAction.login();
    })

    $("#username").live("blur", function () {

        var username = $("#username").val();
        if (username == "请输入邮箱地址" || username == "") {
            $("#username").val('请输入邮箱地址');
        }
    }).live("focus", function () {
        var username = $("#username").val();
        if (username == "请输入邮箱地址" || username == "") {
            $("#username").val('');
        }
    })

    $("#pwd").live("blur", function () {
        var pwd = $("#pwd").val();
        if (pwd == "请输入邮箱密码" || pwd == "") {
            $("#pwd").val('请输入邮箱密码');
        }

    }).live("focus", function () {
        var pwd = $("#pwd").val();
        if (pwd == "请输入邮箱密码" || pwd == "") {
            $("#pwd").val('');
        }
    })

})

var UserAction =
{
    login: function () {
        var username = $("#username").val();
        var pwd = $("#pwd").val();
        var LetterNumber = $("#LetterNumber").val();
        var MessageNumber = $("#MessageNumber").val();
        if (username == "" || username == "请输入邮箱地址") {
            ShowMsg("username", "邮箱地址不能为空！");
            return;
        }
        if (pwd == "" || pwd.length < 6) {
            ShowMsg("pwd", "你的邮箱密码不能小于六个字符");
            return;
        }

        $.ajax(
        {
            url: string.Format("/UserAPI/UserLogin?Email={0}&Pwd={1}", username, pwd), //请求验证页面
            type: "post", //请求方式 可换为post  注意验证页面接收方式
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            success: function (data) {
                if (data.isSuccess) {
                    var htmlstr = string.Format("<a href=\"/User/Account\"><img src=\"{0}\" /></a> <a href=\"/User/Account\"><span class=\"green\">{1}</span></a><a href=\"/User/logout\">退出</a>", data.Avatar, data.Nickname);
                    $("#person").append(htmlstr);
                    $("#person").addClass("person");
                    $("#fpic").hide();
                    $("#login").hide();
                }
                else {
                    alert(data.Message);
                }
            },
            error: function (data) {                // 请求失败后的回调  
                alert(data);
            }
        })
    }
}

